<?php
/**
 * @author Evica Ilieva
 * @date 2012-06-29
 */
class NasaApiImplDAO implements RepositoryDAO{
	/**
	 * Get Document Object by doi
	 *
	 * @param String $doi
	 */
	public function searchByDoi($doi){
		
		$request_url = str_replace('[LOOKFOR]', 'doi='.$doi, RepositoryConnectionProperty::$_url);
		RepositoryConnectionProperty::$_url = $request_url;
		
		//Connect to the Repository
		$conn = new RepositoryConnection();
		
		//The connection returns an XML object
		$xml=$conn->getConnection();
			
		//Create empty objects for Document, Metadata and File
		$document = new Document();
		$metadata = new Metadata();
		$file = new File();
		$keywords = array();
		
		foreach ($xml->attributes() as $a => $b) {
		
			if ($a == 'selected') {
				$count = $b;
				break;
			}
		}
		
		if ($count == 1) {
		
			$record = $xml->record;
			$bibcode = $record->bibcode;
			$metadata->setTitle((string)$record->title);
			$metadata->setTitleAscii(utf8_deaccent((string)$record->title));
			$journal = $record->journal;
			if (strstr($journal, ","))
			{ 
				$metadata->setSecondaryTitle(substr($journal, 0, strpos($journal, ',')));
			}

			$eprintid = $record->eprintid;
			//echo 'eprint ID:'.$journal;
			if (!empty($eprintid)) 
			{
				$eprintid = substr($eprintid, strpos($eprintid, ":")+1);
			}
			if (strstr($journal, "arXiv")) 
			{
				$eprintid = substr($journal, strpos($journal, ":")+1);
			}

			
			if (!empty($record->DOI)) 
				$metadata->setDoi((string)$record->DOI);

			$metadata->setVolume((string)$record->volume);
			$metadata->setPages( (string)$record->page);
			//$record->page = $record->bibcode;
			if (!empty ($record->lastpage))
            {
                   	$metadata->setPages($metadata->getPages().'-'.$record->lastpage);
            }

            $metadata->setAffiliation((string)$record->affiliation);

			$metadata->setYear(date('Y-m-d', strtotime($record->pubdate)));

			$metadata->setAbstract((string)$record->abstract);
			$metadata->setAbstractAscii(utf8_deaccent((string)$record->abstract));
			$nasa_url = $record->url;
			
			foreach($record->link as $links) {
			
				foreach ($links->attributes() as $a => $b) {
			
					if ($a == 'type' && $b == 'EJOURNAL') {
						$ejournal_url = $links->url;
					} elseif ($a == 'type' && $b == 'PREPRINT') {
						$preprint_url = $links->url;
					} elseif ($a == 'type' && $b == 'GIF') {
						$gif_url = $links->url;
					}
				}
			}
			$authors = $record->author;
			
			if (!empty($authors)) {
			
				foreach ($authors as $author) {
			
					$name_array[] = str_replace(",", "", $author);
				}
			}
			
			if (isset($name_array)) 
			{
				$metadata->setAuthors(join (", ", $name_array));
				$metadata->setAutorsAscii(utf8_deaccent((string)join (", ", $name_array)));
			}
			
			$keywords = $record->keywords;
			$keywords_array = array();
			$i=0;
			if (!empty($keywords)) {
			
				foreach ($keywords as $keyword) {
			
					//$keywords_array[] = $keyword->keyword;
					$keyword = new Keyword();
					$keyword->setKeyword((string)$keyword->keyword);
					$keyword->setSelected(true);
					$keywords_array[$i]=$keyword;
				}
			}
			
			if (isset($keywords_array)) 
			{
				$metadata->setKeywords($keywords_array);
			}
			$uid_array = array();
			if (!empty($bibcode)) $uid_array[] = "NASAADS:$bibcode";
			if (!empty($eprintid)) $uid_array[] = "ARXIV:$eprintid";
			$document->setUid(join("|", $uid_array));
			
			$url_array = array();
			$url_array[] = $nasa_url;
			if (!empty($eprintid)) $url_array[] = "http://arxiv.org/abs/$eprintid";
			$metadata->setUrl(join("|", $url_array));
		}
		
		$document->setMetadata($metadata);
		$document->setFile($file);
		
		return $document;
		
	}
	
	/**
	 * Get Document Object by uid
	 * @param String $uid
	*/
	public function searchByUid($uid){

		$request_url = str_replace('[LOOKFOR]', 'bibcode='.$uid, RepositoryConnectionProperty::$_url);
		RepositoryConnectionProperty::$_url = $request_url;
		
		//Connect to the Repository
		$conn = new RepositoryConnection();
		
		//The connection returns an XML object
		$xml=$conn->getConnection();
			
		//Create empty objects for Document, Metadata and File
		$document = new Document();
		$metadata = new Metadata();
		$file = new File();
		$keywords = array();
		
		foreach ($xml->attributes() as $a => $b) {
		
			if ($a == 'selected') {
				$count = $b;
				break;
			}
		}
		
		if ($count == 1) {
		
			$record = $xml->record;
			$bibcode = $record->bibcode;
			$metadata->setTitle((string)$record->title);
			$metadata->setTitleAscii(utf8_deaccent((string)$record->title));
			$journal = $record->journal;
			if (strstr($journal, ","))
			{
				$metadata->setSecondaryTitle(substr($journal, 0, strpos($journal, ',')));
			}
		
			$eprintid = $record->eprintid;
			//echo 'eprint ID:'.$journal;
			if (!empty($eprintid))
			{
				$eprintid = substr($eprintid, strpos($eprintid, ":")+1);
			}
			if (strstr($journal, "arXiv"))
			{
				$eprintid = substr($journal, strpos($journal, ":")+1);
			}
		
				
			if (!empty($record->DOI))
				$metadata->setDoi((string)$record->DOI);
		
			$metadata->setVolume((string)$record->volume);
			$metadata->setPages( (string)$record->page);
			//$record->page = $record->bibcode;
			if (!empty ($record->lastpage))
			{
				$metadata->setPages($metadata->getPages().'-'.$record->lastpage);
			}
		
			$metadata->setAffiliation((string)$record->affiliation);
		
			$metadata->setYear(date('Y-m-d', strtotime($record->pubdate)));
		
			$metadata->setAbstract((string)$record->abstract);
			$metadata->setAbstractAscii(utf8_deaccent((string)$record->abstract));
			$nasa_url = $record->url;
				
			foreach($record->link as $links) {
					
				foreach ($links->attributes() as $a => $b) {
						
					if ($a == 'type' && $b == 'EJOURNAL') {
						$ejournal_url = $links->url;
					} elseif ($a == 'type' && $b == 'PREPRINT') {
						$preprint_url = $links->url;
					} elseif ($a == 'type' && $b == 'GIF') {
						$gif_url = $links->url;
					}
				}
			}
			$authors = $record->author;
				
			if (!empty($authors)) {
					
				foreach ($authors as $author) {
						
					$name_array[] = str_replace(",", "", $author);
				}
			}
				
			if (isset($name_array))
			{
				$metadata->setAuthors(join (", ", $name_array));
				$metadata->setAutorsAscii(utf8_deaccent((string)join (", ", $name_array)));
			}
				
			$keywords = $record->keywords;
			$keywords_array = array();
			$i=0;
			if (!empty($keywords)) {
					
				foreach ($keywords as $keyword) {
						
					//$keywords_array[] = $keyword->keyword;
					$keyword = new Keyword();
					$keyword->setKeyword((string)$keyword->keyword);
					$keyword->setSelected(true);
					$keywords_array[$i]=$keyword;
				}
			}
				
			if (isset($keywords_array))
			{
				$metadata->setKeywords($keywords_array);
			}
			$uid_array = array();
			if (!empty($bibcode)) $uid_array[] = "NASAADS:$bibcode";
			if (!empty($eprintid)) $uid_array[] = "ARXIV:$eprintid";
			$document->setUid(join("|", $uid_array));
				
			$url_array = array();
			$url_array[] = $nasa_url;
			if (!empty($eprintid)) $url_array[] = "http://arxiv.org/abs/$eprintid";
			$metadata->setUrl(join("|", $url_array));
		}
		
		$document->setMetadata($metadata);
		$document->setFile($file);
		
		return $document;
	}
	
	public function findDoiByISSN($issn, $title){}
	
	public function searchByTitle($title){}
}